package com.muyu.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.text.Convert;
import com.muyu.common.core.utils.ObjUtils;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.product.domain.RuleAttrInfo;
import com.muyu.product.domain.model.RuleAttrAddModel;
import com.muyu.product.domain.model.RuleInfoAddModel;
import com.muyu.product.domain.req.RuleInfoQueryReq;
import com.muyu.product.domain.resp.RuleInfoResp;
import com.muyu.product.service.RuleAttrInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.muyu.product.mapper.RuleInfoMapper;
import com.muyu.product.domain.RuleInfo;
import com.muyu.product.service.RuleInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 商品规格Service业务层处理
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Slf4j
@Service
public class RuleInfoServiceImpl extends ServiceImpl<RuleInfoMapper, RuleInfo>  implements RuleInfoService {

    @Autowired
    private RuleAttrInfoService ruleAttrInfoService;

    /**
     * 查询商品规格列表
     *
     * @param ruleInfo 商品规格
     * @return 商品规格
     */
    @Override
    public List<RuleInfo> list(RuleInfo ruleInfo) {
        LambdaQueryWrapper<RuleInfo> queryWrapper = new LambdaQueryWrapper<>();


        if (ObjUtils.notNull(ruleInfo.getName())){
            queryWrapper.like(RuleInfo::getName, ruleInfo.getName());
        }

        if (ObjUtils.notNull(ruleInfo.getStatus())){
            queryWrapper.eq(RuleInfo::getStatus, ruleInfo.getStatus());
        }





        return list(queryWrapper);
    }

    /**
     * 添加规格信息
     *
     * @param ruleInfoAddModel 规格添加模型
     *
     * @return 时候成功
     */
    @Override
    public boolean save (RuleInfoAddModel ruleInfoAddModel) {
        RuleInfo ruleInfo = RuleInfo.addModelBuild(ruleInfoAddModel, SecurityUtils::getUsername);
        boolean save = this.save(ruleInfo);
        ruleAttrInfoService.saveBatch(
                ruleInfoAddModel.getRuleAttrList().stream()
                        .map(ruleAttrAddModel -> RuleAttrInfo.addModelBuild(ruleAttrAddModel, ruleInfo::getId, SecurityUtils::getUsername))
                        .toList()
        );
        return save;
    }

    @Override
    public TableDataInfo<RuleInfoResp> queryList (RuleInfoQueryReq ruleInfoQueryReq) {
        List<RuleInfo> list = this.list(RuleInfo.queryBuild(ruleInfoQueryReq));
        List<RuleInfoResp> ruleInfoRespList = list.stream()
                .map(ruleInfo -> RuleInfoResp.infoBuild(ruleInfo, ruleId -> {
                        LambdaQueryWrapper<RuleAttrInfo> queryWrapper = new LambdaQueryWrapper<>();
                        queryWrapper.eq(RuleAttrInfo::getRuleId, ruleId);
                        return ruleAttrInfoService.list(queryWrapper).stream().map(RuleAttrAddModel::infoBuild).toList();
                    }))
                .toList();
        boolean isPage = ruleInfoQueryReq.getParams().get("isPage") == null || Convert.toBool(ruleInfoQueryReq.getParams().get("isPage"), true);

        return TableDataInfo.<RuleInfoResp>builder()
                .rows(ruleInfoRespList)
                .total(isPage ? new PageInfo<>(list).getTotal() : 0)
                .build();
    }
}
